//修改用户头像和用户昵称
$(function() {

	// 校验用户手机号,nickNameFlag,nickTipNull nickTipError,保存按钮save_button
	var formdata = new FormData();// FormData,提交到后台
	function verifyNickName() {
		var nickName = $("#nickname").val();
		nickName = nickName.replace(/(^\s*)|(\s*$)/g, "");
		if (nickName == "") {
			$("#nickTipError").css("display", "none");
			$("#nickTipNull").css("display", "block");
			$("#nickNameFlag").attr("value", "false");
		} else if (!(/^[a-zA-Z0-9_]{1,}$/).test(nickName)
				|| nickName.indexOf(" ") != -1) {
			$("#nickTipNull").css("display", "none");
			$("#nickTipError").css("display", "block");
			$("#nickNameFlag").attr("value", "false");
		} else {
			$("#nickTipNull").css("display", "none");
			$("#nickTipError").css("display", "none");
			$("#nickNameFlag").attr("value", "true");
			formdata.set("nickName", $("#nickname").val());
		}
	}
	$("#nickname").blur(function() {
		verifyNickName();
	})

	// 校验用户图片
	$("#uploadImages").change(function() {
		addFiles();
	})
	// 回写图片数据
	fileStack = [];// 存放图片文件的数组
	function addFiles() {
		var files = document.querySelector("input[type=file]");
		var filelist = files.files;// 选择图片列表
		console.log("【filelist】", filelist);
		if (filelist.length == 0) {
			formdata.set("file", filelist);
			console.log("【filelist】", filelist);
		}
		console.log("【filelist】" + filelist.length);
		$.each(filelist, function(i, file) {
			var reader = new FileReader();
			reader.onload = function(e) {
				var result = this.result;
				$("#userPhoto").attr("src", result);
				verifyPhoto(file);
			};
			reader.readAsDataURL(file);
		});
	}
	// photoTip photoErrorTip photoSizeTip
	function verifyPhoto(file) {
		if (!/\.(jpg|png|JPG|PNG)$/.test(file.name)) {
			$("#photoTip").css("display", "none");
			$("#photoSizeTip").css("display", "none");
			$("#photoErrorTip").css("display", "block");
			$("#photoFormatFlag").attr("value", "false");
			return;
		}
		if (file.size > 1024 * 1024 * 3) {
			$("#photoTip").css("display", "none");
			$("#photoErrorTip").css("display", "none");
			$("#photoSizeTip").css("display", "block");
			$("#photoFormatFlag").attr("value", "false");
			return;
		}
		$("#photoErrorTip").css("display", "none");
		$("#photoSizeTip").css("display", "none");
		$("#photoTip").css("display", "block");
		$("#photoFormatFlag").attr("value", "true");// 图片标记,用户图片可以为空,只校验是否是
		fileStack.push(file);
		formdata.set("file", file);
	}

	// 提交数据
	$("#save_button").click(function() {
		verifyNickName();	
		console.log("【fileStack该数组长度】" + fileStack.length);
		var photoFormatFlag = $("#photoFormatFlag").attr("value");
		var nicknameFlag = $("#nickNameFlag").attr("value");
		if (nicknameFlag == "true" && photoFormatFlag == "true") {
			console.log("【数据校验成功】");
			formdata.set("uId", $("#uId").attr("value"));
			submitData(formdata);
		} else {
			console.log("【数据校验失败】");
		}
	})
	// 提交数据
	function submitData(formdata) {
		$.ajax({
			type : 'POST',
			url : "/tenement/userModMsg.action",
			dataType : 'json',
			data : formdata,
			cache : false,
			processData : false,
			contentType : false,
			success : function(responseStr) {
				if (responseStr == "1") {
					alert("保存成功");
					
				} else {
					alert("保存失败");
				}
				 location.reload();
			},
			error : function(responseStr) {
				alert("未知错误");
			}
		});
	}
})